import aniBase from "../aniBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class aniProgress extends aniBase {

    @property
    chargeSpeed:number = 0.2;
    @property
    playSpeed:number = 0.5;

    private _start:number = 0;
    private _end:number = 0;
    private _chargeTime:number = 0;

    private chargeTime:number;
    private startValue:number;
    private endValue:number;

    private onChargeEnd:any;
    private onAniEnd:any;

    playFunc(start, end, speed){
        this._start =  start;
        this._end = end;
        this.playBase(speed);
        this._chargeTime++;
    }

    play(start, end, chargeTime = 0){
        this.chargeTime = chargeTime;
        this.startValue = start;
        this.endValue = end;
        this._chargeTime = 0;

        if(this.chargeTime == 0)
            this.playFunc(start, end, this.playSpeed);
        else
            this.playFunc(start, 1, this.chargeSpeed);
    }

    process(dt){
        this.node.getComponent(cc.ProgressBar).progress = this._start + dt * (this._end - this._start);
    }

    Done(){
        if(this._chargeTime < this.chargeTime)
        {
            if(this.onChargeEnd)
                this.onChargeEnd();
            this.playFunc(0, 1, this.chargeSpeed);
        }
        else if(this._chargeTime == this.chargeTime)
        {
            if(this.onChargeEnd)
                this.onChargeEnd();
            this.playFunc(0, this.endValue, this.playSpeed);
        }
        else
        {
            if(this.onAniEnd)
                this.onAniEnd();
            this.node.getComponent(cc.ProgressBar).progress = this.endValue;
        }
    }
}
